#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

// 统计字符数
int countCharacters(const string& text) {
    return text.size();
}

// 统计单词数（以空格或标点分隔）
int countWords(const string& text) {
    int count = 0;
    bool inWord = false;
    for (char ch : text) {
        if (isalpha(ch)) {
            if (!inWord) {
                count++;
                inWord = true;
            }
        } else {
            inWord = false;
        }
    }
    return count;
}

// 统计句子数（以 . ? ! 结尾）
int countSentences(const string& text) {
    int count = 0;
    for (char ch : text) {
        if (ch == '.' || ch == '?' || ch == '!') {
            count++;
        }
    }
    return count;
}

int main(int argc, char* argv[]) {
    if (argc != 3) {
        cerr << "Usage: wc.exe [option] [filename]" << endl;
        cerr << "Options: -c count characters, -w count words, -s count sentences" << endl;
        return 1;
    }

    string option = argv[1];
    string filename = argv[2];

    ifstream file(filename);
    if (!file) {
        cerr << "CAN NOT open file: " << filename << endl;
        return 1;
    }

    string content((istreambuf_iterator<char>(file)),
                   istreambuf_iterator<char>());

    if (option == "-c") {
        cout << "Character count: " << countCharacters(content) << endl;
    } else if (option == "-w") {
        cout << "Word count: " << countWords(content) << endl;
    } else if (option == "-s") {
        cout << "Sentence count: " << countSentences(content) << endl;
    } else {
        cerr << "Nan: " << option << endl;
        cerr << "Use -c, -w or -s" << endl;
        return 1;
    }

    return 0;
}
